﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLUProject.Domain;
using DLUProject.Services;
using System.Text;
using ColorLife.Core.Helper;
public partial class CommonControl_MainMenuTop : BaseUserControl
{
    public IServices<WorkGroup> _workgroupService { get; set; }
    public IServices<Function> _functionService { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
    }
    private List<Function> GetByWorkGroupID(int idWorkGroup)
    {
        return _functionService.All().Where(c => c.WorkGroupID == idWorkGroup).ToList();
    }
    public string LoadWorkgroupFunction()
    {
        StringBuilder html = new StringBuilder();
        var db = _workgroupService.All().Where(c => c.SystemID == Session["SystemID"].ToInt()).ToList();
        
        foreach (var item in db.Where(c => c.ParentID == 0))
        {
            html.Append("<li class='" + CurrentLink(item.Name) + "'>");
            html.Append("<a href=\"" + UrlHelper.PortalURL + item.UrlFull + "\" title='" + item.Name + "' " + CountSubMenu(db, item.WorkGroupID) + "><span>" + item.Name + "</span>");
            html.Append(GetCaret(db, item.WorkGroupID) + "</a>");
            var functions = GetByWorkGroupID(item.WorkGroupID);
            html.Append("<ul class='dropdown-menu'>");
            html.Append(SubMenu(db, item.WorkGroupID));
            foreach (var i in functions)
            {
                html.Append("<li ><a href= '" + UrlHelper.PortalURL+ item.UrlFull + "/Default.aspx?controller=" + i.Url + "' title='" + i.Name + "'>" + i.Name + "</a></li>");
            }
           
            html.Append("</ul>");

            html.Append("</li>");
        }
        return html.ToString();
    }
    private string SubMenu(List<WorkGroup> db, int parentID)
    {
        var e = db.FindAll(x => x.ParentID == parentID);
        StringBuilder html = new StringBuilder();
      //  html.Append("<ul class='dropdown-menu'>");
        if (db.Count > 0)
        {
            foreach (var item in e)
            {
                html.Append(" <li " + GetSubmenuChild(db, item.WorkGroupID) + ">");
                html.Append("   <a href='" +UrlHelper.PortalURL + item.UrlFull + "'>" + item.Name + "</a>");
                //html.Append(SubMenu(db, item.WorkGroupID));
                var functions = GetByWorkGroupID(item.WorkGroupID);
                html.Append("<ul class='dropdown-menu'>");
                foreach (var i in functions)
                {
                    html.Append("<li ><a href= '"+  UrlHelper.PortalURL+  item.UrlFull + "/Default.aspx?controller=" + i.Url + "' title='" + i.Name + "'>" + i.Name + "</a></li>");
                }
              
                html.Append("</ul>");
                html.Append("</li>");
            }
        }
         
      //  html.Append("</ul>");
        return html.ToString();
    }
    public string GetSubmenuChild(List<WorkGroup> db, int parentID)
    {
        var e = db.FindAll(x => x.ParentID == parentID);
        var f = GetByWorkGroupID(parentID);
        string html = "class='dropdown-submenu'";
        if (e.Count > 0 || f.Count >0)
            return html;
        return string.Empty;
    }
    private bool CountSubWK(List<WorkGroup> db, int parentID)
    {
        var e = db.FindAll(x => x.ParentID == parentID);
        var f = GetByWorkGroupID(parentID);
        return e.Count > 0 || f.Count > 0;
    }
    private string GetCaret(List<WorkGroup> db, int parentID)
    {
        var e = db.FindAll(x => x.ParentID == parentID);
        var f = GetByWorkGroupID(parentID);
        string html = "<span class='caret'></span>";
        if (e.Count > 0||f.Count> 0)
            return html;
        return string.Empty;
    }
    string CountSubMenu(List<WorkGroup> db, int parentID)
    {
        var e = db.FindAll(x => x.ParentID == parentID);
        var f = GetByWorkGroupID(parentID);
        string html = " data-toggle='dropdown' class='dropdown-toggle' ";
        if (e.Count  > 0 || f.Count> 0)
            return html;
        return string.Empty;
    }
    string CurrentLink(string pg)
    {
        var cls = "";

        //var nodeUrl = pg;
        //var startIndx = nodeUrl.LastIndexOf("/Admin") > 0 ? nodeUrl.LastIndexOf("/Admin") : 0;
        //var c = pg.Substring(startIndx, pg.Length - startIndx);
        //var endIndx = pg.LastIndexOf("/") > 0 ? pg.LastIndexOf("/") : pg.Length;
        //var nodeDir = pg.Substring(startIndx, endIndx - startIndx);

        //if (HttpContext.Current.Request.RawUrl.IndexOf(nodeUrl, StringComparison.OrdinalIgnoreCase) != -1 ||
        //    (HttpContext.Current.Request.RawUrl.IndexOf(nodeDir, StringComparison.OrdinalIgnoreCase) != -1) &&
        //    !nodeDir.EndsWith("/Admin"))
        //{
        //    cls = "active";
        //}

        //// if "page" has its own subfolder (comments, extensions etc.) 
        //// select parent tab when navigating through child tabs
        //startIndx = nodeDir.LastIndexOf("/");
        //if (startIndx > 0)
        //{
        //    nodeDir = nodeDir.Substring(0, startIndx);
        //    if (HttpContext.Current.Request.RawUrl.ToLower().Contains(nodeDir.ToLower() + "/") && nodeDir.Substring(1, nodeDir.Length - 1).IndexOf("/") > 0)
        //    {
        //        cls = "active";
        //    }
        //}
        if (HttpContext.Current.Request.RawUrl.Contains(pg)) cls = "active";
        cls = "";
        return cls;
    }
    public static string CurrentUrl(string pg)
    {
        if (HttpContext.Current.Request.Path.ToLower().Contains(pg.ToLower()))
        {
            return "class=\"active\"";
        }
        return "";
    }
}